package com.clear.subject.application.mq;

import com.alibaba.fastjson.JSON;
import com.clear.subject.domain.entity.SubjectLikedBO;
import com.clear.subject.domain.service.SubjectLikedDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class SubjectLikedConsumer {

    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "jc.club.direct", type = ExchangeTypes.DIRECT),
            value = @Queue(name = "subject.liked.queue", durable = "true"),
            key = {"liked.success"}
    ))
    public void onMassage(String msg) {
        log.info("接受点赞mq，消息 ==>" + msg);
        SubjectLikedBO subjectLikedBO = JSON.parseObject(msg, SubjectLikedBO.class);
        subjectLikedDomainService.syncLikedByMessage(subjectLikedBO);
    }
}
